package cn.ruituo.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author oldliu
 * @since 1.0
 */
public class HelloServlet extends HttpServlet {
    public HelloServlet(){
        System.out.println("HelloServlet构造了");
    }
    @Override
    public void init() throws ServletException {
        System.out.println("Hello servet被初始化了。。。");
    }

    @Override
    public void destroy() {
        System.out.println("hello servlet销毁了。。。");
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了service....");
        super.service(req, resp);
        System.out.println("结束了service");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了get");
        //req.setCharacterEncoding("utf-8");
        //req.getParameter()
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>这是你的伊达，我的servlet</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1>");
        out.print("这是HelloServlet的get请求");
        out.print("</h1>");
        out.print("</body>");
        out.print("</html>");

    }

    //接受put请求
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入了post");
        resp.setContentType("text/html;charset=UTF-8");
        //req.getParameter()
        PrintWriter out = resp.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>这是你的伊达，我的servlet</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h1>");
        out.print("这是HelloServlet的post请求");
        out.print("</h1>");
        out.print("</body>");
        out.print("</html>");
    }
}
